Scala的Eval树2.11.7

时间:2015-11-02 19:00:49

标签: scala reflection eval

我正在尝试在Scala中实现JSON-RPC服务器,并希望使用注释标记我的远程方法。如果方法本身不应该担心输入参数验证也会很好,所以我想把这个验证放在注释中。我最终得到了这个:

MainActivity.class

远程方法注释如下:

package com.example.TodoList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.example.TodoList.db.TaskContract;
import com.example.TodoList.db.TaskDBHelper;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ListAdapter listAdapter;
    private TaskDBHelper helper;
    private Toolbar toolbar;
    private Button btnIconTextTabs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        updateUI();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        btnIconTextTabs = (Button) findViewById(R.id.btnIconTextTabs);
        btnIconTextTabs.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final EditText inputField = new EditText(this);
        builder.setNegativeButton("Cancel", null);


        switch (item.getItemId()) {
            case R.id.action_add_task:
                builder.setTitle("Add an article to your shopping list");
                builder.setMessage("What would you like to add?");
                builder.setView(inputField);
                builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String task = inputField.getText().toString();

                        helper = new TaskDBHelper(MainActivity.this);
                        SQLiteDatabase db = helper.getWritableDatabase();
                        ContentValues values = new ContentValues();

                        values.clear();
                        values.put(TaskContract.Columns.TASK, task);

                        db.insertWithOnConflict(TaskContract.TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE);
                        updateUI();

                    }
                });

                builder.create().show();
                return true;

            case R.id.action_remove_task:
                builder.setTitle("Remove an article from the shopping list");
                builder.setMessage("Did you found this article?");
                builder.setNegativeButton("Remove", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        String task = inputField.getText().toString();
                        helper = new TaskDBHelper(MainActivity.this);
                        SQLiteDatabase db = helper.getWritableDatabase();
                        ContentValues values = new ContentValues();
                        values.clear();
                        updateUI();
                    }
                });

            case R.id.action_show_mylocation:
                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                Log.d("MyTagGoesHere", "This is my log message at the debug level here");
                //Intent intent=new Intent(this,LbsGeocodingActivity.class);
                //startActivity(intent);

                Intent GeoLocationIntent = new Intent(MainActivity.this, GeoActivity.class);
                //myIntent.putExtra("key", value); //Optional parameters

                MainActivity.this.startActivity(GeoLocationIntent);




        }

        builder.create().show();
        return true;

    }

    private void updateUI() {
        helper = new TaskDBHelper(MainActivity.this);
        SQLiteDatabase sqlDB = helper.getReadableDatabase();
        Cursor cursor = sqlDB.query(TaskContract.TABLE,
                new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
                null, null, null, null, null);

        listAdapter = new SimpleCursorAdapter(
                this,
                R.layout.task_view,
                cursor,
                new String[]{TaskContract.Columns.TASK},
                new int[]{R.id.taskTextView},
                0
        );

        this.setListAdapter(listAdapter);
    }

    public void onDoneButtonClick(View view) {
        View v = (View) view.getParent();
        TextView taskTextView = (TextView) v.findViewById(R.id.taskTextView);
        String task = taskTextView.getText().toString();

        String sql = String.format("DELETE FROM %s WHERE %s = '%s'",
                TaskContract.TABLE,
                TaskContract.Columns.TASK,
                task);


        helper = new TaskDBHelper(MainActivity.this);
        SQLiteDatabase sqlDB = helper.getWritableDatabase();
        sqlDB.execSQL(sql);
        updateUI();
    }

    public void onSubmitPriceClick(View view) {
        Intent SubmitPriceIntent = new Intent(MainActivity.this, SubmitPriceActivity.class);
        MainActivity.this.startActivity(SubmitPriceIntent);
    }

    public void onScrollViewButtonClick(View view) {
        Intent SubmitPriceIntent = new Intent(MainActivity.this, IconTextTabsActivity.class);
        MainActivity.this.startActivity(SubmitPriceIntent);
    }

    public void onWebViewButtonClick(View view) {
        Intent intent = new
                Intent(Intent.ACTION_VIEW,
                Uri.parse("http://www.batprice.com:1337"));
        startActivity(intent);
        finish();
    }


    public void onGeoLocationButtonClick(View view) {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Log.d("MyTagGoesHere", "This is my log message at the debug level here");
        Intent GeoLocationIntent = new Intent(MainActivity.this, GeoActivity.class);
        MainActivity.this.startActivity(GeoLocationIntent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnIconTextTabs:
                startActivity(new Intent(MainActivity.this, IconTextTabsActivity.class));
                break;
        }
    }
}

现在我正在尝试提取{case ...}部分并将其评估为检查入站值的函数。但我能用反射得到的只是一棵树:

class RPCMethod(validators: (String, PartialFunction[Any, Boolean])*) extends StaticAnnotation

那么如何从这个树中获取有效的函数对象?

0 个答案:

没有答案