本文整理汇总了Java中java.beans.Expression类的典型用法代码示例。如果您正苦于以下问题:Java Expression类的具体用法?Java Expression怎么用?Java Expression使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Expression类属于java.beans包,在下文中一共展示了Expression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instantiate
import java.beans.Expression; //导入依赖的package包/类
protected Expression instantiate(Object oldInstance, Encoder out)
{
Class oldClass = (Class) oldInstance;
// Due to the special handling of String instances in the Encoder
// this Expression does not lead to further class resolutions.
if (oldClass == String.class)
return new Expression(oldClass, "", "getClass", null);
// This Expression will lead to the class resolution of String.class.
if (oldClass == Class.class)
return new Expression(oldClass, String.class, "getClass", null);
// This Expression will lead to the class resolution of Class.class.
return new Expression(oldClass, Class.class, "forName",
new Object[] { oldClass.getName() });
}
示例2: instantiate
import java.beans.Expression; //导入依赖的package包/类
protected Expression instantiate(Object oldInstance, Encoder out)
{
Class type = oldInstance.getClass().getComponentType();
// oldInstance is expected to be an array, then
// getClass().getComponentType() should lead
// to its component type.
assert (type != null);
// Not handling primitive types in a special way here
// causes that Class.forName("int") is built as an Expression
// later which would cause an exception if executed. A special
// handling to avoid the execution for primitive types can be
// java.beans.Encoder.writeExpression() .
return new Expression(
oldInstance,
Array.class,
"newInstance",
new Object[] {
type,
new Integer(Array.getLength(oldInstance)) });
}
示例3: instantiate
import java.beans.Expression; //导入依赖的package包/类
protected Expression instantiate(Object oldInstance, Encoder out)
{
Class oldClass = (Class) oldInstance;
// Due to the special handling of String instances in the Encoder
// this Expression does not lead to further class resolutions.
if (oldClass == String.class)
return new Expression(oldClass, "", "getClass", null);
// This Expression will lead to the class resolution of String.class.
if (oldClass == Class.class)
return new Expression(oldClass, String.class, "getClass", null);
// This Expression will lead to the class resolution of Class.class.
return new Expression(oldClass, Class.class, "forName",
new Object[] { oldClass.getName() });
}
示例4: instantiate
import java.beans.Expression; //导入依赖的package包/类
@Override
protected Expression instantiate( Object oldInstance, Encoder out ) {
Class type = oldInstance.getClass();
if ( !Modifier.isPublic( type.getModifiers() ) )
throw new IllegalArgumentException( "Could not instantiate instance of non-public class: " + oldInstance );
for ( Field field : type.getFields() ) {
int mod = field.getModifiers();
if ( Modifier.isPublic( mod ) && Modifier.isStatic( mod ) && Modifier.isFinal( mod ) && ( type == field.getDeclaringClass() ) ) {
try {
if ( oldInstance == field.get( null ) )
return new Expression( oldInstance, field, "get", new Object[]{null} );
} catch ( IllegalAccessException exception ) {
throw new IllegalArgumentException( "Could not get value of the field: " + field, exception );
}
}
}
throw new IllegalArgumentException( "Could not instantiate value: " + oldInstance );
}
示例5: testDiamond
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testDiamond() throws Exception {
if (SourceVersion.latestSupported().ordinal() >= 7) {
compile("import java.util.List; import java.util.LinkedList; public class A{ List<Integer> bar = new LinkedList<>(); }");
SymbolTable symTable = getSymbolTable();
symTable.pushScope();
SymbolType st = new SymbolType(getClassLoader().loadClass("A"));
symTable.pushSymbol("a", ReferenceType.VARIABLE, st, null);
FieldAccessExpr expr = (FieldAccessExpr) ASTManager.parse(Expression.class, "a.bar");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("java.util.List", type.getName());
Assert.assertNotNull(type.getParameterizedTypes());
Assert.assertEquals("java.lang.Integer", type.getParameterizedTypes().get(0).getName());
}
}
示例6: testTargetInference
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testTargetInference() throws Exception {
compile("import java.util.Collections; " + "import java.util.List; "
+ "public class A { public static void processStringList(List<String> stringList) {}}");
SymbolType st = new SymbolType(getClassLoader().loadClass("A"));
SymbolTable symTable = getSymbolTable();
symTable.pushScope();
symTable.pushSymbol("A", ReferenceType.TYPE, st, null);
MethodCallExpr expr =
(MethodCallExpr) ASTManager.parse(Expression.class, "A.processStringList(Collections.emptyList());");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("void", type.getName());
}
示例7: testLambdaExpressionsWithExplicitArgs
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testLambdaExpressionsWithExplicitArgs() throws Exception {
if (SourceVersion.latestSupported().ordinal() >= 8) {
String BCode = "public interface B { public int execute(int c); } ";
compile("public class A{ public void run(B b){} " + BCode + "}");
SymbolType st = new SymbolType(getClassLoader().loadClass("A"));
SymbolTable symTable = getSymbolTable();
symTable.pushScope();
symTable.pushSymbol("a", ReferenceType.TYPE, st, null);
MethodCallExpr expr = (MethodCallExpr) ASTManager.parse(Expression.class, "a.run((int d)->d+1)");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("A$B", type.getMethod().getParameterTypes()[0].getName());
}
}
示例8: testMethodReferencesToConstructors
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testMethodReferencesToConstructors() throws Exception {
if (SourceVersion.latestSupported().ordinal() >= 8) {
compile("import java.util.HashSet; public class A{ public static void foo(B b) {} public interface B{ public Object get();}}");
SymbolTable symTable = getSymbolTable();
symTable.pushSymbol("this", ReferenceType.TYPE, new SymbolType(getClassLoader().loadClass("A")), null);
MethodCallExpr expr = (MethodCallExpr) ASTManager.parse(Expression.class, "A.foo(HashSet::new)");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("foo", type.getMethod().getName());
MethodReferenceExpr arg1 = (MethodReferenceExpr) expr.getArgs().get(0);
SymbolType methodType = (SymbolType) arg1.getReferencedMethodSymbolData();
Assert.assertNotNull(methodType);
Assert.assertEquals("get", methodType.getMethod().getName());
}
}
示例9: testTargetInferenceShouldFail
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testTargetInferenceShouldFail() throws Exception {
compile("import java.util.Collections; " + "import java.util.List; "
+ "public class A { public static <T> void processStringList(T[] args) {}}");
SymbolType st = new SymbolType(getClassLoader().loadClass("A"));
SymbolTable symTable = getSymbolTable();
symTable.pushScope();
symTable.pushSymbol("A", ReferenceType.TYPE, st, null);
try {
MethodCallExpr expr = (MethodCallExpr) ASTManager.parse(Expression.class,
"A.processStringList(Collections.emptyList());");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
fail("should not be reached: type=" + expr.getSymbolData().getMethod());
} catch (NoSuchExpressionTypeException e) {
Assert.assertTrue(e.getMessage(), e.getMessage()
.contains("Ops! The method call A.processStringList(Collections.emptyList()) is not resolved"));
}
}
示例10: testStatic
import java.beans.Expression; //导入依赖的package包/类
/**
* The test checks the correct static method is initialized
*/
public void testStatic() throws Exception {
SampleBean theBean = new SampleBean();
Expression expr = new Expression(SampleBean.class, "create",
new Object[] { "hello", theBean });
Object result = expr.getValue();
if (result != null && result instanceof SampleBean) {
SampleBean bean = (SampleBean) result;
assertEquals("hello", bean.getText());
assertEquals(theBean, bean.getObject());
} else {
fail("Cannot instantiate an instance of Bean class by "
+ "static method.");
}
}
示例11: testMethodReferencesToAnArrayItem
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testMethodReferencesToAnArrayItem() throws Exception {
if (SourceVersion.latestSupported().ordinal() >= 8) {
compile("import java.util.Arrays; public class A{}");
SymbolType st = new SymbolType(String.class);
st.setArrayCount(1);
SymbolTable symTable = getSymbolTable();
symTable.pushScope();
symTable.pushSymbol("stringArray", ReferenceType.TYPE, st, null);
symTable.pushSymbol("this", ReferenceType.TYPE, new SymbolType(getClassLoader().loadClass("A")), null);
MethodCallExpr expr = (MethodCallExpr) ASTManager.parse(Expression.class,
"Arrays.sort(stringArray, String::compareToIgnoreCase)");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("sort", type.getMethod().getName());
MethodReferenceExpr arg1 = (MethodReferenceExpr) expr.getArgs().get(1);
SymbolType methodType = (SymbolType) arg1.getReferencedMethodSymbolData();
Assert.assertNotNull(methodType);
Assert.assertEquals("compare", methodType.getMethod().getName());
}
}
示例12: testGenericMethodsExplicitTypeInvocation
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testGenericMethodsExplicitTypeInvocation() throws Exception {
compile("import java.util.ArrayList; import java.io.Serializable;"
+ " public class A { public static <T> T pick(T a1, T a2) { return a2; }}");
SymbolTable symTable = getSymbolTable();
ASTSymbolTypeResolver.getInstance().setSymbolTable(symTable);
symTable.pushScope();
SymbolType st = new SymbolType(getClassLoader().loadClass("A"));
symTable.pushSymbol("A", ReferenceType.TYPE, st, null);
MethodCallExpr expr = (MethodCallExpr) ASTManager.parse(Expression.class,
"A.<Serializable>pick(\"d\", new ArrayList<String>())");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("java.io.Serializable", type.getName());
}
示例13: testTargetInference2
import java.beans.Expression; //导入依赖的package包/类
public void testTargetInference2() throws Exception {
compile("import java.util.Collections; " + "import java.util.List; "
+ "public class A { public static void processAList(List<A> stringList) {}}");
SymbolType st = new SymbolType(getClassLoader().loadClass("A"));
SymbolTable symTable = getSymbolTable();
symTable.pushScope();
symTable.pushSymbol("A", ReferenceType.TYPE, st, null);
MethodCallExpr expr =
(MethodCallExpr) ASTManager.parse(Expression.class, "A.processAList(Collections.emptyList());");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expressionAnalyzer.visit(expr, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("void", type.getName());
}
示例14: testReferencesToInnerClasses
import java.beans.Expression; //导入依赖的package包/类
@Test
public void testReferencesToInnerClasses() throws Exception {
compile(true, "public class OuterClass { public class InnerClass { } }");
SymbolTable symTable = getSymbolTable();
ASTSymbolTypeResolver.getInstance().setSymbolTable(symTable);
symTable.pushScope();
SymbolType st = new SymbolType(getClassLoader().loadClass("OuterClass"));
symTable.pushSymbol("outerObject", ReferenceType.VARIABLE, st, null);
org.walkmod.javalang.ast.expr.Expression expr = (org.walkmod.javalang.ast.expr.Expression) ASTManager
.parse(Expression.class, "outerObject.new InnerClass()");
HashMap<String, Object> ctx = new HashMap<String, Object>();
expr.accept(expressionAnalyzer, ctx);
SymbolType type = (SymbolType) expr.getSymbolData();
Assert.assertNotNull(type);
Assert.assertEquals("OuterClass$InnerClass", type.getName());
}
示例15: instantiate
import java.beans.Expression; //导入依赖的package包/类
@Override
protected Expression instantiate(Object oldInstance, Encoder out) {
byte[] e = (byte[]) oldInstance;
return new Expression(e, ByteArrayPersistenceDelegate.class,
"decode",
new Object[] { ByteArrayPersistenceDelegate.encode(e) });
}